package com.springboot.cloud.demos.producer.dao;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.cloud.demos.entity.po.Moment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import javax.ws.rs.QueryParam;
import java.util.Date;
import java.util.Map;

@Repository
@Mapper
public interface MomentMapper extends BaseMapper<Moment> {
    @Select("SELECT a.id, a.title,a.created_time as createdTime,a.requirement,a.appointment_date as appointmentDate," +
            "a.position,a.topic,a.support_num as supportNum,a.sign_up_num as signUpNum,a.comment_num as commentNum," +
            "a.type,a.subject,a.location,longitude,latitude," +
            "c.id as userId,c.mobile,c.username,c.nickname,c.sex,c.real_man as realMan," +
            "c.pretty ,c.avatar,c.description,c.vip_status as vipStatus from t_moment a " +
            "left join t_user c on c.id=a.user_id where a.id = ${id}")
    Map<String, Object> selectMap(@Param("id")String id);

    @Select("SELECT a.id, a.title,a.created_time as createdTime,a.requirement,a.appointment_date as appointmentDate," +
            "a.position,a.topic,a.support_num as supportNum,a.sign_up_num as signUpNum,a.comment_num as commentNum," +
            "a.type,a.subject,a.location,longitude,latitude," +
            "c.id as userId,c.mobile,c.username,c.nickname,c.sex,c.real_man as realMan," +
            "c.pretty ,c.avatar,c.description,c.vip_status as vipStatus from t_moment a " +
            "left join t_user c on c.id=a.user_id ${ew.customSqlSegment}")
    @Override
    IPage<Map<String, Object>> selectMapsPage(IPage page, @Param("ew") Wrapper queryWrapper);


    @Select("SELECT a.id, a.title,a.created_time as createdTime,a.requirement,a.appointment_date as appointmentDate," +
            "a.position,a.topic,a.support_num as supportNum,a.sign_up_num as signUpNum,a.comment_num as commentNum," +
            "a.type,a.subject,a.location,longitude,latitude," +
            "c.id as userId,c.mobile,c.username,c.nickname,c.sex,c.real_man as realMan," +
            "c.pretty ,c.avatar,c.description,c.vip_status as vipStatus from t_moment a " +
            "left join t_user c on c.id=a.user_id " +
            "left join t_follow d on d.follow_user_id=a.user_id" +
            " ${ew.customSqlSegment}")
    IPage<Map<String, Object>> followList(IPage page, @Param("ew") Wrapper queryWrapper);


    @Select(" SELECT  a.id, a.title,a.created_time as createdTime,a.requirement," +
            "        a.appointment_date as appointmentDate,a.position,a.topic," +
            "        a.support_num as supportNum,a.sign_up_num as signUpNum,a.comment_num as commentNum," +
            "        a.type,a.subject,a.location,longitude,latitude,c.id as userId," +
            "c.mobile,c.username,c.nickname,c.sex,c.real_man as realMan," +
            "        c.pretty ,c.avatar,c.description,c.vip_status as vipStatus ,ROUND(" +
            "        6378.138 * 2 * ASIN(" +
            "            SQRT(" +
            "                POW(" +
            "                    SIN(" +
            "                        (" +
            "                            ${latitude} * PI() / 180 - a.latitude * PI() / 180" +
            "                        ) / 2" +
            "                    )," +
            "                    2" +
            "                ) + COS(${latitude} * PI() / 180) * COS(a.latitude * PI() / 180) * POW(" +
            "                    SIN(" +
            "                        (" +
            "                            ${longitude} * PI() / 180 - a.longitude * PI() / 180" +
            "                        ) / 2" +
            "                    )," +
            "                    2" +
            "                )" +
            "            )" +
            "        ) * 1000" +
            "    ) AS distance" +
            "   from t_moment as a" +
            "     left join t_user c on c.id=a.user_id" +
            " ${ew.customSqlSegment} order by distance asc")
    IPage<Map<String, Object>> nearbyList(Page page, @Param("ew")QueryWrapper queryWrapper, @Param("latitude") float latitude,@Param("longitude") float longitude);

    @Select("SELECT a.id, a.title,a.created_time as createdTime,a.requirement,a.appointment_date as appointmentDate," +
            "a.position,a.topic,a.support_num as supportNum,a.sign_up_num as signUpNum,a.comment_num as commentNum," +
            "a.type,a.subject,a.location,longitude,latitude," +
            "c.id as userId,c.mobile,c.username,c.nickname,c.sex,c.real_man as realMan," +
            "c.pretty ,c.avatar,c.description,c.vip_status as vipStatus from t_moment a " +
            "left join t_user c on c.id=a.user_id ${ew.customSqlSegment}")
    IPage<Map<String, Object>> broadcastList(Page page, @Param("ew")QueryWrapper queryWrapper);

    @Select("SELECT a.id, a.title,a.created_time as createdTime,a.requirement,a.appointment_date as appointmentDate," +
            "a.position,a.topic,a.support_num as supportNum,a.sign_up_num as signUpNum,a.comment_num as commentNum," +
            "a.type,a.subject,a.location,longitude,latitude," +
            "c.id as userId,c.mobile,c.username,c.nickname,c.sex,c.real_man as realMan," +
            "c.pretty ,c.avatar,c.description,c.vip_status as vipStatus from t_moment a " +
            "left join t_user c on c.id=a.user_id ${ew.customSqlSegment}")
    IPage<Map<String, Object>> recommendList(Page page,@Param("ew") QueryWrapper queryWrapper);


    @Select("SELECT a.id, a.title,a.created_time as createdTime,a.requirement,a.appointment_date as appointmentDate," +
            "a.position,a.topic,a.support_num as supportNum,a.sign_up_num as signUpNum,a.comment_num as commentNum," +
            "a.type,a.subject,a.location,longitude,latitude," +
            "c.id as userId,c.mobile,c.username,c.nickname,c.sex,c.real_man as realMan," +
            "c.pretty ,c.avatar,c.description,c.vip_status as vipStatus from t_moment a " +
            "left join t_user c on c.id=a.user_id where  deleted='N' and a.type=3 and a.user_id='${userId}' and a.created_time>'${createdTime}' order by  a.created_time asc limit 1")
    Map<String, Object> nextWidthUser(@Param("userId") String userId,@Param("createdTime") String createdTime);

    @Select("SELECT a.id, a.title,a.created_time as createdTime,a.requirement,a.appointment_date as appointmentDate," +
            "a.position,a.topic,a.support_num as supportNum,a.sign_up_num as signUpNum,a.comment_num as commentNum," +
            "a.type,a.subject,a.location,longitude,latitude," +
            "c.id as userId,c.mobile,c.username,c.nickname,c.sex,c.real_man as realMan," +
            "c.pretty ,c.avatar,c.description,c.vip_status as vipStatus from t_moment a " +
            "left join t_user c on c.id=a.user_id where  deleted='N' and a.type=3 and   a.user_id='${userId}' and a.created_time<'${createdTime}' order by  a.created_time desc limit 1")
    Map<String, Object> prevWidthUser(@Param("userId") String userId,@Param("createdTime") String createdTime);
    @Select("SELECT a.id, a.title,a.created_time as createdTime,a.requirement,a.appointment_date as appointmentDate," +
            "a.position,a.topic,a.support_num as supportNum,a.sign_up_num as signUpNum,a.comment_num as commentNum," +
            "a.type,a.subject,a.location,longitude,latitude," +
            "c.id as userId,c.mobile,c.username,c.nickname,c.sex,c.real_man as realMan," +
            "c.pretty ,c.avatar,c.description,c.vip_status as vipStatus from t_moment a " +
            "left join t_user c on c.id=a.user_id where  deleted='N' and a.type=3 and  a.created_time>'${createdTime}' order by  a.created_time asc limit 1")
    Map<String, Object> next( @Param("createdTime") String createdTime);

    @Select("SELECT a.id, a.title,a.created_time as createdTime,a.requirement,a.appointment_date as appointmentDate," +
            "a.position,a.topic,a.support_num as supportNum,a.sign_up_num as signUpNum,a.comment_num as commentNum," +
            "a.type,a.subject,a.location,longitude,latitude," +
            "c.id as userId,c.mobile,c.username,c.nickname,c.sex,c.real_man as realMan," +
            "c.pretty ,c.avatar,c.description,c.vip_status as vipStatus from t_moment a " +
            "left join t_user c on c.id=a.user_id where  deleted='N' and a.type=3 and a.created_time<'${createdTime}' order by  a.created_time desc limit 1")
    Map<String, Object> prev( @Param("createdTime") String createdTime);
}